<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */
//用户端登陆
//基本思路：当需要用户登陆方可访问时，跳转至此页。先检测用户环境（微信端、手机端、pc端、小程序端）
//微信端和小程序端统一处理：如果存在$_SESSION['openid']则根据openid来登陆
//微信端如果没能获取到openid，则类比普通手机端处理
//普通手机端和pc端统一处理
//总结精炼：登陆（注册）前不判断，登陆（注册）后，如果session中有openid，则把当前粉丝与登陆的用户绑定
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
//cfc('core')->checklogin();
$return['account']=$_SESSION['account'];
$return['extradata']=array();
$setting=uni_setting('register');
$setting['sfyzx']=intval($setting['sfyzx']);
if($_RQ['op']=='get'){
	$return['item']=array();
	$return['title']='注册';
	$setting['sfyzx'] = $setting['sfyzx'] ? $setting['sfyzx'] : '2';
	$return['item']=array('setting'=>$setting['sfyzx']);
	exi($return);
}
if($_RQ['op']=='sendcode'){
	$post=get_postdata();
	$mobile=$post['mobile'];
	$code=rand(1000,9999);
	$_SESSION['smscode']=$code;
	$content=array('code'=>$code);
	$setting=uni_setting('member_setting');
	$mid=$setting['sms_mb']['login'];
	$result=cfc('jy_alisms')->send($mobile,$content,$mid);
	if($result){
		exi('success');
	}else{
		exi('error');
	}

}
if($_RQ['op']=='forget'){
//exi('ff','error');
	if($_RQ['postdata']){
	//找回密码
	$post=get_postdata();
		//手机验证码
	if(!$post['mobile']){
		exi('请输入手机号码！','error');
	}
	if(!$post['password'] || !$post['password1']){
		exi('请输入密码！','error');
	}
	if($post['password'] != $post['password1']){
		exi('两次输入密码不一致！','error');
	}
	if(!$_SESSION['smscode'] || $_SESSION['smscode']!=trim($post['code'])){
		exi('验证码错误！','error');
	}
	$uid=pdo_getcolumn('core_members',array('mobile'=>trim($post['mobile']),'uniacid'=>$_SESSION['uniacid']),'id');
	if(!$uid){
		exi('该手机号未注册!','error');
	}
	//print_r($post);exit;
	$update=array('mobile'=>trim($post['mobile']),'salt'=>random(10));
	$update['password']=get_password(trim($post['password']),$update['salt']);
	
	pdo_update('core_members',$update,array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$post['mobile']));
	
		exi('修改成功！','success','jy_weishop/account.login/');
	
	
	}
	$return['type']=1;
	$return['post']=array();
	exi($return);
}
if($_RQ['op']=='qq_login'){
	if(!$_AR['system']['qq_login']){
		exi('系统暂不支持QQ登陆！','error','./index.php');
	}
	if($_RQ['step']=='back'){
		if($_RQ['openid']){
			//获得openid，为用户登陆
			$openid=trim($_RQ['openid']);
			$uniacid=pdo_getcolumn('core_open_user',array('open_id'=>'1002','open_uid'=>$openid),'uniacid');
			if(!$uniacid){
				$setting=core_setting('zhuce');
				if(!$setting['open']){
					exi('当前QQ尚未绑定本站账号！系统自动注册失败:本站暂未开启注册！','error','./index.php');
				}
				do{
					$username='qq_'.random(10);
					$check=pdo_getcolumn('core_users',array('name'=>$username),'id');
				}while($check);
				$member=array('name'=>$username);
				$uniacid=cfc('user')->create_user($member);
				$user=array(
					'uniacid'=>$uniacid,
					'open_id'=>'1002',
					'open_uid'=>$openid,
				);
				pdo_insert('core_open_user',$user);
			}
			//登陆用户
			cfc('user')->login($uniacid);
			//跳转首页
			$_SESSION['access']=array();
			exi('授权登陆成功！','','./index.php');
		}else{
			$_SESSION['access']=array();
			exi('授权登录失败！','','./index.php');
		}
		
	}
	//微信授权登陆
	$back_url=SITEROOT.'custom/index.php?plugin=core&action=member.login&op=qq_login&step=back';
	$url=cloud_request('cloud.login.qq',array('back_url'=>$back_url));
	exi('','',$url);
}
?>